import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.util.Vector;

/**
 * Created by berkbozkurt on 07/03/15.
 */
public class GameInteraction extends JPanel {


    private LifeView lifeView;
    // Single paddle object
    private Paddle paddle;

    // Game Object Containers
    private Vector<Ball> balls;
    public float fps;
    public String lastPowerUp;
    private Vector<Brick> bricks;
    private Vector<PowerUp> powerUps;
    private Vector<Brick> destroyedBricks;
    private Vector<Ball> destroyedBalls;
    private Vector<PowerUp> destroyedPowerups;

    // Controllers
    private LevelManager levelManager;
    private GameController gameController;

    // Constructor
    public GameInteraction() {

        // View Construction
        this.setMaximumSize(new Dimension(600, 550));
        this.setSize(new Dimension(600, 550));
        this.setMinimumSize(new Dimension(600, 550));
        this.setPreferredSize(new Dimension(600, 550));
        this.setBackground(Color.BLACK);
        // Initiate Brick and Ball ArrayList
        bricks = new Vector<Brick>();
        balls = new Vector<Ball>();
        powerUps = new Vector<PowerUp>();

        destroyedBricks = new Vector<Brick>();
        destroyedBalls = new Vector<Ball>();
        destroyedPowerups = new Vector<PowerUp>();



        // Create Ball
        Ball ball = new Ball(this.getSize());
        ball.x = 300;
        ball.y = 400;
        ball.xVelocity = 0f;
        ball.yVelocity = -2f;

        // Add the first ball to container
        balls.add(ball);

        // Create Paddle
        paddle = new Paddle(this.getSize(), "default");
        paddle.x = this.getWidth() / 2 - paddle.width / 2;
        paddle.y = this.getHeight() - paddle.height;

        // Create Level Manager
        levelManager = new LevelManager(this.getSize());

        // Get GameController
        gameController = GameController.getInstance();

        // Update views
        this.validate();
        this.repaint();

    }


    public void createPaddle(String type) {
        float x = paddle.x;
        float y = paddle.y;

        paddle = new Paddle(this.getSize(), type);
        paddle.x = x;
        paddle.y = y;
    }

    public void destroyBrick(int i) {
        destroyedBricks.add(bricks.get(i));
    }

    public void destroyBall(int i) {
        destroyedBalls.add(balls.get(i));
    }

    public void destroyPowerUp(int i) {
        destroyedPowerups.add(powerUps.get(i));
    }

    public void removeDestroyedBricks() {
        for (Brick b : destroyedBricks) {
            bricks.remove(b);
        }
        destroyedBricks.clear();
    }
    public void removeDestroyedBalls() {
        for (Ball b : destroyedBalls) {
            balls.remove(b);
        }
        destroyedBalls.clear();
    }

    public void removeDestroyedPowerUps() {
        for (PowerUp p: destroyedPowerups) {
            powerUps.remove(p);
        }
        destroyedPowerups.clear();
    }
    public void constructLevel(int level) {
        for (Ball b : destroyedBalls) {
            b.yVelocity = -2f;
        }
        bricks.addAll(levelManager.constructLevel(level));
    }

    @Override
    protected void paintComponent(Graphics g)
    {

        super.paintComponent(g);

        // Paint Ball
        g.setColor(Color.WHITE);

        for(Ball b : balls) {
            g.fillOval((int) (b.x - b.width / 2), (int) (b.y - b.height / 2), b.width, b.height);
        }

        // Paint Paddle
        g.setColor(Color.BLACK);
        g.drawImage(paddle.getImage(), (int) (paddle.x - paddle.width / 2), (int) (paddle.y - paddle.height / 2), null);

        for(Brick b : bricks) {
            g.drawImage(b.getImage(), b.getLocation().x, b.getLocation().y, null);
        }

        for(PowerUp p : powerUps) {
            g.drawImage(p.getImage(), (int) p.getX(), (int)p.getY(), null);
        }
        g.setColor(Color.WHITE);
        if(fps != 0) {
            g.drawString("FPS: " + fps, 15, 10);
        }

        g.drawString(lastPowerUp, 80, 10);
        g.dispose();
    }


    public Vector<PowerUp> getPowerUps() { return powerUps; }
    public Vector<Ball> getBalls() { return balls; }
    public Paddle getPaddle() { return paddle; }
    public Vector<Brick> getBricks() {
        return bricks;
    }
    public LifeView getLifeView() {return lifeView; }
}
